<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">

    

    <title>
      ES6中Map，Set与数组和对象的比较 | 江月夜 
    </title>

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    
      <meta name="author" content="江月夜">
    
    

    <meta name="description" content="前言在es6中常用的数据集合有这几种，Object、Array、Map、Set。他们之间有什么区别？在什么情况下选用哪种数据集合类型？下来看这几种数据集合的使用比较。  Object：Object构造函数创建一个对象包装器。 Array：Array对象是用于构造数组的全局对象，数组是类似于列表的高阶对象。 Map：Ma">
<meta name="keywords" content="es6">
<meta property="og:type" content="article">
<meta property="og:title" content="ES6中Map，Set与数组和对象的比较 | 江月夜">
<meta property="og:url" content="http://yoursite.com/2018/10/29/es6-mapset/index.html">
<meta property="og:site_name" content="江月夜">
<meta property="og:description" content="前言在es6中常用的数据集合有这几种，Object、Array、Map、Set。他们之间有什么区别？在什么情况下选用哪种数据集合类型？下来看这几种数据集合的使用比较。  Object：Object构造函数创建一个对象包装器。 Array：Array对象是用于构造数组的全局对象，数组是类似于列表的高阶对象。 Map：Map 对象保存键值对。任何值(对象或者原始值) 都可以作为一个键或一个值。 Set">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-10-29T10:23:14.128Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="ES6中Map，Set与数组和对象的比较 | 江月夜">
<meta name="twitter:description" content="前言在es6中常用的数据集合有这几种，Object、Array、Map、Set。他们之间有什么区别？在什么情况下选用哪种数据集合类型？下来看这几种数据集合的使用比较。  Object：Object构造函数创建一个对象包装器。 Array：Array对象是用于构造数组的全局对象，数组是类似于列表的高阶对象。 Map：Map 对象保存键值对。任何值(对象或者原始值) 都可以作为一个键或一个值。 Set">
    
    
    
      <link rel="icon" type="image/x-icon" href="/favicon.png">
    
    <link rel="stylesheet" href="/css/uno.css">
    <link rel="stylesheet" href="/css/highlight.css">
    <link rel="stylesheet" href="/css/archive.css">
    <link rel="stylesheet" href="/css/china-social-icon.css">

</head>
<body>

    <span class="mobile btn-mobile-menu">
        <i class="icon icon-list btn-mobile-menu__icon"></i>
        <i class="icon icon-x-circle btn-mobile-close__icon hidden"></i>
    </span>

    

<header class="panel-cover panel-cover--collapsed">


  <div class="panel-main">

  
    <div class="panel-main__inner panel-inverted">
    <div class="panel-main__content">

        

        <h1 class="panel-cover__title panel-title"><a href="/" title="link to homepage">江月夜</a></h1>
        <hr class="panel-cover__divider" />

        
        <p class="panel-cover__description">
          腹有诗书气自华
        </p>
        <hr class="panel-cover__divider panel-cover__divider--secondary" />
        

        <div class="navigation-wrapper">

          <nav class="cover-navigation cover-navigation--primary">
            <ul class="navigation">

              
                
                <li class="navigation__item"><a href="/#blog" title="" class="blog-button">首页</a></li>
              
                
                <li class="navigation__item"><a href="/about" title="" class="">关于</a></li>
              
                
                <li class="navigation__item"><a href="/archive" title="" class="">归档</a></li>
              

            </ul>
          </nav>

          <!-- ----------------------------
To add a new social icon simply duplicate one of the list items from below
and change the class in the <i> tag to match the desired social network
and then add your link to the <a>. Here is a full list of social network
classes that you can use:

    icon-social-500px
    icon-social-behance
    icon-social-delicious
    icon-social-designer-news
    icon-social-deviant-art
    icon-social-digg
    icon-social-dribbble
    icon-social-facebook
    icon-social-flickr
    icon-social-forrst
    icon-social-foursquare
    icon-social-github
    icon-social-google-plus
    icon-social-hi5
    icon-social-instagram
    icon-social-lastfm
    icon-social-linkedin
    icon-social-medium
    icon-social-myspace
    icon-social-path
    icon-social-pinterest
    icon-social-rdio
    icon-social-reddit
    icon-social-skype
    icon-social-spotify
    icon-social-stack-overflow
    icon-social-steam
    icon-social-stumbleupon
    icon-social-treehouse
    icon-social-tumblr
    icon-social-twitter
    icon-social-vimeo
    icon-social-xbox
    icon-social-yelp
    icon-social-youtube
    icon-social-zerply
    icon-mail

-------------------------------->

<!-- add social info here -->



<nav class="cover-navigation navigation--social">
  <ul class="navigation">

    
      <!-- Github -->
      <li class="navigation__item">
        <a href="https://github.com/ajiu9" title="">
          <i class='icon icon-social-github'></i>
          <span class="label">GitHub</span>
        </a>
      </li>
    

    <!-- China social icon -->
    <!--
    
      <li class="navigation__item">
        <a href="" title="">
          <i class='icon cs-icon-douban'></i>
          <span class="label">Douban</span>
        </a>
      </li>

      <li class="navigation__item">
        <a href="" title="">
          <i class='icon cs-icon-weibo'></i>
          <span class="label">Weibo</span>
        </a>
      </li>

    -->



  </ul>
</nav>



        </div>

      </div>

    </div>

    <div class="panel-cover--overlay"></div>
  </div>
</header>

    <div class="content-wrapper">
        <div class="content-wrapper__inner entry">
            

<article class="post-container post-container--single">

  <header class="post-header">
    
    <h1 class="post-title">ES6中Map，Set与数组和对象的比较</h1>

    

    <div class="post-meta">
      <time datetime="2018-10-29" class="post-meta__date date">2018-10-29</time> 

      <span class="post-meta__tags tags">

          
            <font class="categories">
            &#8226; 分类:
            <a class="categories-link" href="/categories/es6/">es6</a>
            </font>
          

          
             &#8226; 标签:
            <font class="tags">
              <a class="tags-link" href="/tags/es6/">es6</a>
            </font>
          

      </span>
    </div>
    
    

  </header>

  <section id="post-content" class="article-content post">
    <h4 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h4><p>在es6中常用的数据集合有这几种，Object、Array、Map、Set。他们之间有什么区别？在什么情况下选用哪种数据集合类型？下来看这几种数据集合的使用比较。</p>
<ul>
<li>Object：<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object" target="_blank" rel="noopener">Object</a>构造函数创建一个对象包装器。</li>
<li>Array：<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array" target="_blank" rel="noopener">Array</a>对象是用于构造数组的全局对象，数组是类似于列表的高阶对象。</li>
<li>Map：<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map" target="_blank" rel="noopener">Map</a> 对象保存键值对。<code>任何值</code>(对象或者原始值) 都可以作为一个键或一个值。</li>
<li>Set：<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Set" target="_blank" rel="noopener">Set</a> 对象允许你存储任何类型的<code>唯一值</code>，无论是原始值或者是对象引用。</li>
</ul>
<h4 id="Map与数组比较"><a href="#Map与数组比较" class="headerlink" title="Map与数组比较"></a>Map与数组比较</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> map = <span class="keyword">new</span> <span class="built_in">Map</span>()</span><br><span class="line"><span class="keyword">let</span> arr = <span class="keyword">new</span> <span class="built_in">Array</span>()</span><br><span class="line"></span><br><span class="line"><span class="comment">// add</span></span><br><span class="line">map.set(<span class="string">'a'</span>, <span class="number">1</span>)</span><br><span class="line">arr.push(&#123;</span><br><span class="line">  <span class="string">"a"</span>: <span class="number">1</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'map-array'</span>, map, arr) <span class="comment">// map-array Map(1) &#123;"a" =&gt; 1&#125; [&#123;…&#125;]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// exist</span></span><br><span class="line"><span class="keyword">let</span> map_exist = map.has(<span class="string">'a'</span>)</span><br><span class="line"><span class="keyword">let</span> arr_exist = arr.find(<span class="function"><span class="params">item</span> =&gt;</span> item.a)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'map-array'</span>, map_exist, arr_exist) <span class="comment">// map-array true &#123;a: 1&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// modify</span></span><br><span class="line"></span><br><span class="line">map.set(<span class="string">'a'</span>, <span class="number">2</span>)</span><br><span class="line">arr.forEach(<span class="function"><span class="params">item</span> =&gt;</span> item.a ? item.a = <span class="number">2</span> : <span class="string">''</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'map-array'</span>, map, arr) <span class="comment">// map-array Map(1) &#123;"a" =&gt; 2&#125; [&#123;…&#125;]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// delete</span></span><br><span class="line">map.delete(<span class="string">"a"</span>)</span><br><span class="line"><span class="keyword">let</span> index = arr.findIndex(<span class="function"><span class="params">item</span> =&gt;</span> item.a)</span><br><span class="line">arr.splice(arr.findIndex(<span class="function"><span class="params">item</span> =&gt;</span> item.b), <span class="number">1</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'map-array'</span>, map, arr) <span class="comment">// map-array Map(0) &#123;&#125; []</span></span><br></pre></td></tr></table></figure>
<h4 id="Set与数组比较"><a href="#Set与数组比较" class="headerlink" title="Set与数组比较"></a>Set与数组比较</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> set = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line"><span class="keyword">let</span> arrary = []</span><br><span class="line"></span><br><span class="line"><span class="comment">// add</span></span><br><span class="line"><span class="keyword">var</span> a1 = &#123;</span><br><span class="line">  a: <span class="number">11</span></span><br><span class="line">&#125;</span><br><span class="line">set.add(a1)</span><br><span class="line">arrary.push(&#123;</span><br><span class="line">  <span class="string">'a'</span>: <span class="number">11</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'set-array'</span>, set, arrary) <span class="comment">// set-array Set(1) &#123;&#123;…&#125;&#125; [&#123;…&#125;]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//exist</span></span><br><span class="line"><span class="keyword">let</span> set_exist = set.has(<span class="string">'a'</span>)</span><br><span class="line"><span class="keyword">let</span> arrary_exist = arrary.find(<span class="function"><span class="params">item</span> =&gt;</span> item.a)</span><br><span class="line"><span class="built_in">console</span>.info(<span class="string">'map-array'</span>, set_exist, arrary_exist) <span class="comment">// map-array false &#123;a: 11&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// modify</span></span><br><span class="line">set.forEach(<span class="function"><span class="params">item</span> =&gt;</span> item.a ? item.a = <span class="number">2</span> : <span class="string">''</span>)</span><br><span class="line">arrary.forEach(<span class="function"><span class="params">item</span> =&gt;</span> item.a ? item.a = <span class="number">2</span> : <span class="string">''</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'set-array'</span>, set, arrary) <span class="comment">// set-array Set(1) &#123;&#123;…&#125;&#125; [&#123;…&#125;]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// delete</span></span><br><span class="line">set.forEach(<span class="function"><span class="params">item</span> =&gt;</span> item.a ? set.delete(item) : <span class="string">''</span>)</span><br><span class="line">arrary.splice(arrary.findIndex(<span class="function"><span class="params">item</span> =&gt;</span> item.b), <span class="number">1</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'map-array'</span>, set, arrary) <span class="comment">// map-array Set(0) &#123;&#125; []</span></span><br></pre></td></tr></table></figure>
<h4 id="Map-Set与Object比较"><a href="#Map-Set与Object比较" class="headerlink" title="Map-Set与Object比较"></a>Map-Set与Object比较</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> item = &#123;</span><br><span class="line">  <span class="string">'a'</span>: <span class="number">1</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> map = <span class="keyword">new</span> <span class="built_in">Map</span>()</span><br><span class="line"><span class="keyword">let</span> set = <span class="keyword">new</span> <span class="built_in">Set</span>()</span><br><span class="line"><span class="keyword">let</span> obj = <span class="keyword">new</span> <span class="built_in">Object</span>()</span><br><span class="line"></span><br><span class="line"><span class="comment">// add</span></span><br><span class="line">map.set(<span class="string">'a'</span>, <span class="number">1</span>)</span><br><span class="line">set.add(item)</span><br><span class="line">obj.a = <span class="number">1</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'map-set-object'</span>, map, set, obj) <span class="comment">// map-set-object Map(1) &#123;"a" =&gt; 1&#125; Set(1) &#123;&#123;…&#125;&#125; &#123;a: 1&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// exist</span></span><br><span class="line"><span class="built_in">console</span>.info(&#123;</span><br><span class="line">  map_exist: map.has(<span class="string">'a'</span>),</span><br><span class="line">  set_exist: set.has(item),</span><br><span class="line">  ibj_exist: <span class="string">'a'</span> <span class="keyword">in</span> obj</span><br><span class="line"></span><br><span class="line">&#125;) <span class="comment">// &#123;map_exist: true, set_exist: true, ibj_exist: true&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// modify</span></span><br><span class="line">map.set(<span class="string">'a'</span>, <span class="number">2</span>)</span><br><span class="line">item.a = <span class="number">2</span></span><br><span class="line">obj[<span class="string">'a'</span>] = <span class="number">2</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'map-set-object'</span>, map, set, obj) <span class="comment">// map-set-object Map(1) &#123;"a" =&gt; 2&#125; Set(1) &#123;&#123;…&#125;&#125; &#123;a: 2&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//delete</span></span><br><span class="line">map.delete(<span class="string">'a'</span>)</span><br><span class="line">set.delete(item)</span><br><span class="line"><span class="keyword">delete</span> obj[<span class="string">'a'</span>]</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'map-set-object'</span>, map, set, obj) <span class="comment">// map-set-object Map(0) &#123;&#125; Set(0) &#123;&#125; &#123;&#125;</span></span><br></pre></td></tr></table></figure>
<h4 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h4><p>从上面对数据结构的增查改删中可以看出，能使用Map不用Array,如果要保证数据的唯一性，可以使用Set。</p>

  </section>

  <section class="post-comments">

   <!--PC和WAP自适应版-->
<div id="SOHUCS" sid="请将此处替换为配置SourceID的语句" ></div> 
<script type="text/javascript"> 
(function(){ 
var appid = 'cytl2q04G'; 
var conf = 'prod_c6e77c75d2d767714e129c11e500044d'; 
var width = window.innerWidth || document.documentElement.clientWidth; 
if (width < 960) { 
window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>'); } else { var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("https://changyan.sohu.com/upload/changyan.js",function(){window.changyan.api.config({appid:appid,conf:conf})}); } })(); </script>
    
</section>


</article>


            <footer class="footer">

    <span class="footer__copyright">&copy; 2017 | 由<a href="https://hexo.io/">Hexo</a>| 主题<a href="https://github.com/someus/huno">Huno</a></span>
    
</footer>
        </div>
    </div>

    <!-- js files -->
    <script src="/js/jquery.min.js"></script>
    <script src="/js/main.js"></script>
    <script src="/js/scale.fix.js"></script>
    

    

    <script type="text/javascript" src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <script type="text/javascript"> 
        $(document).ready(function(){
            MathJax.Hub.Config({ 
                tex2jax: {inlineMath: [['[latex]','[/latex]'], ['\\(','\\)']]} 
            });
        });
    </script>


    

    <script src="/js/awesome-toc.min.js"></script>
    <script>
        $(document).ready(function(){
            $.awesome_toc({
                overlay: true,
                contentId: "post-content",
            });
        });
    </script>


    
    
    <!--kill ie6 -->
<!--[if IE 6]>
  <script src="//letskillie6.googlecode.com/svn/trunk/2/zh_CN.js"></script>
<![endif]-->

</body>
</html>
